import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 22652
 * Date: 2024-04-09
 * Time: 15:13
 */
class MyThreadPool {
    private final BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>();

    // 通过这个方法, 来把任务添加到线程池中.
    public void submit(Runnable runnable) throws InterruptedException {
        queue.put(runnable);
    }

    // n 表示线程池里有几个线程.
    // 创建了一个固定数量的线程池.
    public MyThreadPool(int n) {
        for (int i = 0; i < n; i++) {
            Thread t = new Thread(() -> {
                while (true) {
                    try {
                        // 取出任务, 并执行~~
                        Runnable runnable = queue.take();
                        runnable.run();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
            t.start();
        }
    }
}
public class test_2 {
    public static void main(String[] args) throws InterruptedException {
        MyThreadPool pool = new MyThreadPool(4);
        pool.submit(new Runnable() {
            @Override
            public void run() {
                System.out.println("a");
                System.out.println("++++++++++++++++++++++++++++++++++++++++");
            }
        });
        pool.submit(new Runnable() {
            @Override
            public void run() {
                System.out.println("b");
                System.out.println("++++++++++++++++++++++++++++++++++++++++");
            }
        });
        pool.submit(new Runnable() {
            @Override
            public void run() {
                System.out.println("c");
                System.out.println("++++++++++++++++++++++++++++++++++++++++");
            }
        });
    }
}
